package com.wzb.springboot003.controller;

import com.wzb.springboot003.entity.Employee;
import com.wzb.springboot003.service.EmployeeService;
import com.wzb.springboot003.vo.EmployeeVO;
import com.wzb.springbootcommon.utils.PageResult;
import com.wzb.springbootcommon.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

	@Autowired
	private EmployeeService employeeService;

	// 员工列表
	@RequestMapping("/list")
	public R getStudent() {
		return R.ok().data("list",employeeService.list());
	}

	// 分页查询
	@GetMapping("/findByPage/{page}/{pageSize}")
	public R findByPage(@PathVariable("page") Integer page,
	                    @PathVariable("pageSize") Integer pageSize) {
		PageResult<Employee> pr = employeeService.findByPage(page,pageSize);
		return R.ok().data("pr",pr);
	}
	// 条件带分页查询
	@PostMapping("/search/{page}/{pageSize}")
	public R search(@PathVariable("page") Integer page,
	                @PathVariable("pageSize") Integer pageSize,
	                @RequestBody(required = false) EmployeeVO vo){
		PageResult<Employee> pr = employeeService.search(page,pageSize,vo);
		return R.ok().data("pr",pr);
	}
	// 添加员工
	@PostMapping("/add")
	public R add(@RequestBody Employee student){
		boolean flag = employeeService.add(student);
		return flag ? R.ok().message("添加学生成功!") : R.error().message("添加学生失败!");
	}
	
	//修改员工信息
	@PutMapping("/update")
	public R update(@RequestBody Employee student){
		boolean flag = employeeService.updateById(student);
		return flag ? R.ok().message("修改学生成功!") : R.error().message("修改学生失败!");
	}
	
	
	// 删除员工
	@DeleteMapping("/delete/{ids}")
	public R delete(@PathVariable("ids") String ids){  // 1,2,3
		String[] split = ids.split(",");
		List<String> employeeIds = Arrays.asList(split);
		return employeeService.removeByIds(employeeIds) ? R.ok().message("删除学生成功!") : R.error().message("删除学生失败!");
	}
}
